<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible('add')" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="danger" size='small' @click="deleteAllData" class="clearClick">{{$t('清空模板')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="name" :label="$t('模板名字')" min-width="100">
        <template slot-scope="scope">
          <tooltip :html="scope.row.show_name" :content="scope.row.show_name" class="wid190" :refName="scope.row.show_name">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="type" :label="$t('模板类型')" min-width="100">
        <template slot-scope="scope">
          {{ $t(typeList[scope.row.type]) }}
        </template>
      </el-table-column>
      <el-table-column prop="prot" :label="$t('分组依据')" min-width="100">
        <template slot-scope="scope">
          {{ $t(groupList[scope.row.type][scope.row.group]) }}
        </template>
      </el-table-column>
      <el-table-column prop="des" :label="$t('模板描述')" min-width="200">
        <template slot-scope="scope">
          <tooltip :html="scope.row.des" :content="scope.row.des" class="wid190" :refName="scope.row.des">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column align='center' prop="RefCount" :label="$t('引用计数')" width="120">
      </el-table-column>
      <el-table-column align='center' width='170' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible('edit', scope.row)" :class="'edit' + (scope.$index + 1)">{{$t('编辑')}}</el-button>
          <el-button type="success" plain size="mini" @click="showDialogVisible(scope.row)" :class="'details' + (scope.$index + 1)">{{$t('查看详情')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <TempleReportTools v-if="templeReportToolsVisible" ref="refTempleReportToolsVisible"></TempleReportTools>
    <TempleDetails v-if="templeVisible" ref="refTempleVisible"></TempleDetails>
  </div>
</template>
<script>
import { getTemplateArray, delTemplateInfo, clearTemplate } from '@/api/system/logReport/templateReport'
import TempleReportTools from './TempleReportTools'
import TempleDetails from './TempleDetails'

export default {
  name: 'TempleDiyReport',
  components: {
    TempleReportTools,
    TempleDetails
  },
  data () {
    return {
      templeReportToolsVisible: false,
      templeVisible: false,
      tableData: [],
      typeList: {
        'attack-defense': '异常报文攻击',
        'ddos-defense': 'DDOS攻击',
        'av-defense': '病毒防御',
        'content-filter': '工业审计',
        'ips-defense': '入侵防御',
        'event-type': '事件类型',
        'protocol-type': '协议类型'
      },
      groupList: {
        'attack-defense': {
          'sip': '攻击源地址',
          'dip': '受攻击主机',
          'event-type': '威胁攻击',
          'l4type': '受影响协议'
        },
        'ddos-defense': {
          'dip': '受攻击主机'
        },
        'av-defense': {
          'sip': '攻击源地址',
          'dip': '受攻击主机',
          'event-type': '威胁攻击',
          'l4type': '受影响协议'
        },
        'content-filter': {
          'sip': '源地址',
          'dip': '目的地址',
          'event-type': '工业事件',
          'l4type': '工业协议'
        },
        'ips-defense': {
          'sip': '攻击源地址',
          'dip': '受攻击主机',
          'event-type': '威胁攻击',
          'l4type': '受影响协议'
        },
        'event-type': {
          'event-type': '威胁攻击'
        },
        'protocol-type': {
          'l4type': '受影响协议'
        }
      }
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      this.tableData = []
      getTemplateArray().then(res => {
        if (res.status === 0 && res.info && res.info.length > 0) {
          for (let item of res.info) {
            if (item.level === 'user') {
              this.tableData.push(item)
            }
          }
        }
      })
    },
    // 添加按钮
    dialogVisible (type, data) {
      this.templeReportToolsVisible = true
      this.$nextTick(() => {
        this.$refs.refTempleReportToolsVisible.init(data, type)
      })
    },
    showDialogVisible (data) {
      console.log('data',data)
      this.templeVisible = true
      this.$nextTick(() => {
        this.$refs.refTempleVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delTemplateInfo({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    deleteAllData () {
      this.$confirm(this.$t('您是否要清空所有数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        clearTemplate().then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t(res.msg),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    },
    diyTemplateShow (value, str) {
      let reStr = ''
      if (value && value.length > 0) {
        for (let item of value) {
          reStr += item + str
        }
      }
      return reStr
    }
  }
}
</script>
